package org.abacus.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class StringHttpResponse extends HttpResponse<String> {

	public StringHttpResponse(InputStream arg0) {
		super(arg0);
	}

	public String getResponse() throws HttpException {
		String response = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			int b = -1;
			while ((b = is.read()) != -1) {
				baos.write(b);
			}
			response = new String(baos.toByteArray(), getCharsetEncoding());
		} catch (IOException e) {
			throw new HttpException(e.getMessage(), e, "IOException");
		} finally {
			if (null != is) {
				try {
					is.close();
				} catch (IOException e) {
					throw new HttpException(e.getMessage(), e, "IOException");
				}
			}
			if (null != baos) {
				try {
					baos.close();
				} catch (IOException e) {
					throw new HttpException(e.getMessage(), e, "IOException");
				}
			}
		}
		return response;
	}

}
